﻿using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Fa.Kanbox.API;

namespace Fa.Kanbox.API.Controls
{
    
    public class WebBrowserCode : WebBrowser
    {
        public WebBrowserCode()
            : base()
        {
        }
        [Description("API 当获得CODE时发生")]
        public event GetCodeCompletedEventHandler GetCodeCompleted;

        [Description("API client_id")]
        public string ClientID { get; set; }
        [Description("API client_secret")]
        public string ClientSecret { get; set; }


        private string _UserLanguage = "ZH";
        [Description("API 界面语言")]
        [DefaultValue("ZH")]
        public string UserLanguage
        {
            get
            {
                return _UserLanguage;
            }
            set
            {
                _UserLanguage = value;
            }
        }
        [Description("登陆用户名")]
        public string Username { get; set; }

        [Description("登陆密码")]
        public string Password { get; set; }

        [Description("是否自动登陆")]
        [DefaultValue(false)]
        public bool AutoLogin { get; set; }

        private string _AuthUrl = "https://auth.kanbox.com/0/auth?";

        [Description("登陆地址")]
        [DefaultValue("https://auth.kanbox.com/0/auth?")]
        public string AuthUrl
        {
            get
            {
                return _AuthUrl;
            }
            set
            {
                _AuthUrl = value;
            }
        }
        private string _AuthUrl2 = "https://auth.kanbox.com/0/auth.php?";

        [Description("授权地址")]
        [DefaultValue("https://auth.kanbox.com/0/auth.php?")]
        public string AuthUrl2
        {
            get
            {
                return _AuthUrl2;
            }
            set
            {
                _AuthUrl2 = value;
            }
        }

        public void ToGetCode()
        {
            this.Navigate(KbAddress.Combine(
                KbAddress.Auth,
                new
                {
                    response_type = "code",
                    client_id = ClientID,
                    redirect_uri = KbAddress.RedirectUri,
                    user_language = UserLanguage,
                    user_platform = "windows"
                }));
        }
        
        protected virtual void OnGetCodeCompleted(string code, Exception ex)
        {
            if (this.GetCodeCompleted != null)
            {
                this.GetCodeCompleted(new GetCodeCompletedEventArgs { Code = code, Exception = ex });
            }
        }
        protected override void OnNavigating(WebBrowserNavigatingEventArgs e)
        {
            if (e.Url.ToString().StartsWith(KbAddress.RedirectUri))
            {
                e.Cancel = true;
                this.Stop();
                var q = this.Url.Query;
                var index = q.IndexOf('?');
                foreach (var str in q.Substring(index + 1).Split('&'))
                {
                    var strIndex = str.IndexOf('=');
                    if (string.Compare(str.Substring(0, strIndex), "code") == 0)
                    {
                        this.OnGetCodeCompleted(str.Substring(strIndex + 1), null);
                    }
                }
                return;
            }
            base.OnNavigating(e);
        }
    }
}
